<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>Grade Bay</title>
		<link rel="stylesheet" href="gui.css" type="text/css" />
		<!--[if IE 7]>
			<link rel="stylesheet" href="css/ie7.css" type="text/css" />
		<![endif]-->
	</head>
	<body>
		<div class="page">
			<div class="header">
				<a href="index.php" id="logo"><img src="images/gradeBaylogo.png" width="330" height="79" alt=""/></a>
				<ul>
					<li class="selected"><a href="">ADD SUBJECT</a></li>
					
				
				</ul>
			</div>
			<div class="body">
                          

<?php

         /*
          * To change this template, choose Tools | Templates
          * and open the template in the editor.
          */
       include 'PHPClasses/DBAccess.php';
       include 'PHPClasses/Subject.php';
       include 'PHPClasses/GradeCriterion.php';
       include 'PHPClasses/SubjectDBAccess.php';
       
      error_reporting(E_ERROR | E_PARSE);
       
       
       
        if (isset($_POST['submitted']))
       { 
       
        $DBAccess=new SubjectDBAccess();                     
        $Subject=new Subject();
        
        $GradeCriteria1st=new ArrayObject();
        $GradeCriteria2nd=new ArrayObject();
        $GradeCriteria3rd=new ArrayObject();
        $GradeCriteria4th=new ArrayObject();
        
        $Subject->setSubjectName($_POST["subjName"]);
        $Subject->setSubjectLevel($_POST["level"]);
        $Subject->setPassingGrade($_POST["passing"]);
        $Subject->setTeacherInCharge($_POST["teacher"]); //id of Teacher
        
        $OverOrUnder100=FALSE;
        
        $c=$_POST["criteria"];
        $cp=$_POST["critPercent"];
        $c1=$_POST["criteria1"];
        $cp1=$_POST["critPercent1"];
        $c2=$_POST["criteria2"];
        $cp2=$_POST["critPercent2"];
        $c3=$_POST["criteria3"];
        $cp3=$_POST["critPercent3"];
        
     
        
       
        if(array_sum($cp)<100||array_sum($cp)>100||array_sum($cp1)<100||array_sum($cp1)>100||array_sum($cp2)<100||array_sum($cp2)>100
                        ||array_sum($cp3)<100||array_sum($cp3)>100){
            $OverOrUnder100=TRUE;
        }
        
        
        for($i=0;$i<sizeof($c);$i++){
                
                    
                    if($c[$i]==""||$cp[$i]==""){
                           $GradeCriteria1st=null;
                    }
                    
                    else{
                    $GradeCriterion=new GradeCriterion();
                    $GradeCriterion->setName($c[$i]);
                    $GradeCriterion->setPercentage($cp[$i]);
                    $GradeCriterion->setQuarter("1st Quarter");
                    $GradeCriteria1st->append($GradeCriterion);
                    }
                    
                  
        }
        
       
        
        for($i=0;$i<sizeof($c1);$i++){
            
                    if($c1[$i]==""||$cp1[$i]==""){
                           $GradeCriteria2nd=null;
                    }
                    
                    else{
                    $GradeCriterion=new GradeCriterion();
                    $GradeCriterion->setName($c1[$i]);
                    $GradeCriterion->setPercentage($cp1[$i]);
                    $GradeCriterion->setQuarter("2nd Quarter");
                    $GradeCriteria2nd->append($GradeCriterion);
                    }
        }
        
         
        for($i=0;$i<sizeof($c2);$i++){
            
                    if($c2[$i]==""||$cp2[$i]==""){
                           $GradeCriteria3rd=null;
                    }
                    else{
                    $GradeCriterion=new GradeCriterion();
                    $GradeCriterion->setName($c2[$i]);
                    $GradeCriterion->setPercentage($cp2[$i]);
                    $GradeCriterion->setQuarter("3rd Quarter");
                    $GradeCriteria3rd->append($GradeCriterion);
                    }
        }
        
        
         
        for($i=0;$i<sizeof($c3);$i++){
            
                    if($c3[$i]==""||$cp3[$i]==""){
                           $GradeCriteria4th=null;
                    }
                    
                    else{
                    $GradeCriterion=new GradeCriterion();
                    $GradeCriterion->setName($c3[$i]);
                    $GradeCriterion->setPercentage($cp3[$i]);
                    $GradeCriterion->setQuarter("4th Quarter");
                    $GradeCriteria4th->append($GradeCriterion);
                    }
        }
        
        
        
        
       $errormsg=$DBAccess->validate($Subject,$GradeCriteria1st, $GradeCriteria2nd, $GradeCriteria3rd,$GradeCriteria4th,FALSE,$OverOrUnder100);
                                     
        if ($errormsg){ //If any errors display them  
            header("Location: http://localhost/paragox-infinity-introse/addSubject.php?error=".$errormsg."");
        }
        else
        {
           $DBAccess->addToDB($Subject);
            
           $DBAccess->addCriterionToDB($GradeCriteria1st);
           $DBAccess->addCriterionToDB($GradeCriteria2nd);
           $DBAccess->addCriterionToDB($GradeCriteria3rd);
           $DBAccess->addCriterionToDB($GradeCriteria4th);
            
            
            
            echo "<script> 
               if(confirm('Successfully Added! Add some more subjects?')){
                    window.location='http://localhost/paragox-infinity-introse/addSubject.php';
               }
               else{
                window.location='http://localhost/paragox-infinity-introse/viewSubjects.php';
                  }   
               
               </script>"; 
        
        }
   
        
        
        $DBAccess->DBDisconnect(); 
      }       
?>	

                     
                            		
			</div>
			
		</div>
	</body>
</html>  